package com.apes.fa.service;

import com.apes.fa.model.FaAssetBorrowReturnSingle;
import com.apes.fa.model.FaCard;
import com.apes.fa.respository.FaAssetBorrowReturnSingleRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.jpa.spec.model.Group;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.rbac.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fa.service
 * @ClassName: FaAssetBorrowReturnSingleService
 * @Author: PengZiFeng
 * @Description: 资产借还单
 * @CreateTime: 2021-09-06 15:38
 * @Version: 1.0
 */
@Service("faAssetBorrowReturnSingleService")
public class FaAssetBorrowReturnSingleService extends DomainService {

    @Autowired
    private FaAssetBorrowReturnSingleRepository faAssetBorrowReturnSingleRepository;

    /**
     * 保存
     */
    public FaAssetBorrowReturnSingle save(SimpleRequest request){

        FaAssetBorrowReturnSingle faAssetBorrowReturnSingle = request.getO(FaAssetBorrowReturnSingle.class);
        faAssetBorrowReturnSingle.setBusinessState("create");
        return faAssetBorrowReturnSingleRepository.saveAndFlush(faAssetBorrowReturnSingle);

    }

    /**
     * 删除
     */
    public String delete(SimpleRequest request){

        FaAssetBorrowReturnSingle faAssetBorrowReturnSingle = request.getO(FaAssetBorrowReturnSingle.class);
        faAssetBorrowReturnSingleRepository.delete(faAssetBorrowReturnSingle);
        return "删除成功！";

    }

    /**
     * 发货
     */
    public FaAssetBorrowReturnSingle sendGoods(SimpleRequest request){

        FaAssetBorrowReturnSingle faAssetBorrowReturnSingle = request.getO(FaAssetBorrowReturnSingle.class);
        faAssetBorrowReturnSingle.setBusinessState("send");
        FaCard faCard = faAssetBorrowReturnSingle.getFaCard();
        if (faAssetBorrowReturnSingle.getBorrowDept().getCompany().getId()==faCard.getDept().getCompany().getId()){
            faCard.setFaStatus("1");
        }else{
            faCard.setFaStatus("4");
        }
        return faAssetBorrowReturnSingleRepository.saveAndFlush(faAssetBorrowReturnSingle);

    }

    /**
     * 收货
     */
    public FaAssetBorrowReturnSingle receiptGoods(SimpleRequest request){

        FaAssetBorrowReturnSingle faAssetBorrowReturnSingle = request.getO(FaAssetBorrowReturnSingle.class);
        faAssetBorrowReturnSingle.setBusinessState("receipt");
        //收货后变更使用部门
        FaCard faCard = faAssetBorrowReturnSingle.getFaCard();
        faCard.setUseDept(faAssetBorrowReturnSingle.getBorrowDept());
        faCard.setResponsiblePerson(faAssetBorrowReturnSingle.getPrincipal());
        faCard.setCustodian(faAssetBorrowReturnSingle.getCustodian());

        return faAssetBorrowReturnSingleRepository.saveAndFlush(faAssetBorrowReturnSingle);

    }
}
